package com.iot.service;

import com.iot.mapper.DRefMapper;
import com.iot.mapper.DTempMapper;
import com.iot.mapper.DTreeMapper;
import com.iot.mapper.TempMapper;
import com.iot.pojo.MRef;
import com.iot.pojo.MTemp;
import com.iot.pojo.MTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TempServiceImpl implements TempService {
    @Autowired
    private TempMapper mapper;
    @Autowired
    private DTempMapper dmapper;
    @Autowired
    private DTreeMapper dTreeMapper;
    @Autowired
    private DRefMapper dRefMapper;

    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }
    @Override
    public int insert(MTemp mTemp) throws Exception {
        int count = 0;
        count = getRecordCount(" AND APP_ID ="+ mTemp.getAPP_ID()+" AND EMP_NO='" + mTemp.getEMP_NO() + "'");
        if (count > 0)
            throw new Exception("该职工编号已存在");
        count = getRecordCount(" AND CARDNO='" + mTemp.getCARDNO() + "'");
        if (count > 0)
            throw new Exception("该身份证号已存在");
        return mapper.insert(mTemp);
    }

    @Override
    public void update(MTemp mTemp) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID ="+ mTemp.getAPP_ID()+" AND EMP_NO='" + mTemp.getEMP_NO() + "' AND EMP_ID!="+mTemp.getEMP_ID()+"");
        if (count > 0)
            throw new Exception("该职工编号已存在");
        count = getRecordCount(" AND CARDNO='" + mTemp.getCARDNO() + "' AND EMP_ID!="+mTemp.getEMP_ID()+"");
        if (count > 0)
            throw new Exception("该身份证号已存在");
        mapper.update(mTemp);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTemp single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTemp> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {
        return dmapper.fetchByPage(map);
    }


    @Override
    public Map<String, Object> initList(int appId) {
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        Map<String, Object> map = new HashMap<>();
        List<MRef> postList = dRefMapper.getPost(appId);
        map.put("post", postList);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public Map<String, Object> initEdit(int appId) {
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        Map<String, Object> map = new HashMap<>();
        List<MRef> postList = dRefMapper.getPost(appId);
        map.put("post", postList);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public List<Map<String, Object>> getEmpByDeviceId(Map<String,Object> map) {
        return dmapper.getEmpByDeviceId(map);
    }
}
